package com.localjoycommunity.cpdj.project.system.service.impl;

/**
 * @author: aqie
 * @create: 2020-11-29 16:55
 **/


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.project.system.domain.UserWish;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserWishHistoryVo;
import com.localjoycommunity.cpdj.project.system.mapper.ActivityWishMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserWishMapper;
import com.localjoycommunity.cpdj.project.system.service.IUserWishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户心愿Service业务层处理
 *
 * @author leepandar
 * @date 2020-11-16
 */
@Service
public class UserWishServiceImpl extends ServiceImpl<UserWishMapper, UserWish> implements IUserWishService {
    @Autowired
    private UserWishMapper userWishMapper;

    @Autowired
    private ActivityWishMapper activityWishMapper;
    /**
     * 查询用户心愿列表
     *
     * @param userWish 用户心愿
     * @return 用户心愿
     */
    @Override
    public List<UserWish> selectUserWishList(UserWish userWish) {
        return userWishMapper.selectUserWishList(userWish);
    }

    @Override
    public int insertUserWish(UserWish userWish) {
        return userWishMapper.insertUserWish(userWish);
    }


    /**
     * 修改用户心愿
     *
     * @param userWish 用户心愿
     * @return 结果
     */
    @Override
    public int updateUserWish(UserWish userWish) {
        //用户取消
        userWish.setStateCode(0);
        return userWishMapper.updateUserWish(userWish);
    }

    /**
     * 批量删除用户心愿
     *
     * @param ids 需要删除的用户心愿ID
     * @return 结果
     */
    @Override
    public int deleteUserWishByIds(Long[] ids) {
        return userWishMapper.deleteUserWishByIds(ids);
    }

    /**
     * 删除用户心愿信息
     *
     * @param id 用户心愿ID
     * @return 结果
     */
    @Override
    public int deleteUserWishById(Long id) {
        return userWishMapper.deleteUserWishById(id);
    }

    @Override
    @Transactional
    public int applyWish(UserWish userWish) {
        // 1. 查询是否存在
        UserWish one = baseMapper.selectOne(new QueryWrapper<UserWish>().lambda().eq(UserWish::getUserId, userWish.getUserId())
                .eq(UserWish::getWishId, userWish.getWishId()));
        if(one == null){
            // 不存在 用户认领心愿 剩余人数 -1
            activityWishMapper.updateRemainNum(userWish.getWishId(), -1);
            // 2. userWish 表插入一条数据
            userWish.setCreateTime(new Date());
            return baseMapper.insert(userWish);
            // 3. 统计用户活动数???
        }
        return -1;
    }

    /**
     * 查询用户 心愿认领状态
     */
    @Override
    public int isApplyWish( Long wishId){
        return userWishMapper.isApplyWish(wishId).orElse(0);
    }

    @Override
    public List<UserWishHistoryVo> selectUserWishHistory() {
        return userWishMapper.selectUserWishHistory();
    }
}

